package cn.itsource.pethome.system.order.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.order.domain.OrderPetAcquisition;
import cn.itsource.pethome.system.order.domain.OrderPetVo;
import cn.itsource.pethome.system.order.service.IOrderPetAcquisitionService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.system.pet.domain.Pet;
import cn.itsource.pethome.system.pet.domain.SearchMasterMsg;
import cn.itsource.pethome.system.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pethome.system.pet.service.IPetService;
import cn.itsource.pethome.system.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2023-02-09
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IPetService iPetService;
    @Override
    public void delete(Long id) {
        OrderPetAcquisition one = super.findOne(id);
        super.delete(id);
        searchMasterMsgService.reject(one.getSearchMasterMsgId());
    }

    @Override
    public void confirm(OrderPetVo orderPetVo) {
        //订单的状态修改
        Long id = orderPetVo.getId();
        OrderPetAcquisition one = super.findOne(id);
        one.setState(BaseConstants.Order.STATE_SUCCESS);
        one.setPaytype(orderPetVo.getPayType());


        //把数据保存到pet里面
        //将宠物的数据copy到pet里面 注意是下架的   交易价格就是成本价
        Long searchMasterMsgId = one.getSearchMasterMsgId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findOne(searchMasterMsgId);
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setState(BaseConstants.Pet.STATE_DOWN);
        pet.setCostprice(orderPetVo.getMoney().toString());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setCreatetime(new Date());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        iPetService.add(pet);

        one.setPetId(pet.getId());
        super.update(one);//执行修改
    }
}
